package com.example.demojimmer.config;

import cn.hutool.core.io.resource.ResourceUtil;
import com.example.demojimmer.domain.userauth.filter.JwtAuthenticationFilter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Slf4j
@Configuration
@RequiredArgsConstructor
public class JwtConfig {

    public static String publicKey;

    @Value("${jwt.key.public}")
    public void setPublicKey(String publicKey) {
        JwtConfig.publicKey = ResourceUtil.readUtf8Str(publicKey);
    }


    public static String privateKey;

    @Value("${jwt.key.private}")
    public void setPrivateKey(String privateKey) {
        JwtConfig.privateKey = ResourceUtil.readUtf8Str(privateKey);
    }

    //token在header中的key名
    public static String headerkeyName;

    @Value("${jwt.headerkey_name}")
    public void setHeaderkeyName(String headerkeyName) {
        JwtConfig.headerkeyName = headerkeyName;
    }


    /*//解码器
    @Bean
    public JwtDecoder jwtDecoder() {
        return NimbusJwtDecoder.withPublicKey(publicKey).build();
    }

    //编码器
    @Bean
    public JwtEncoder jwtEncoder() {
        JWK jwk = new RSAKey.Builder(publicKey).privateKey(privateKey).build();
        JWKSource<SecurityContext> jwkSet = new ImmutableJWKSet<>(new JWKSet(jwk));
        return new NimbusJwtEncoder(jwkSet);
    }*/

    //把认证过滤器加入管理
    @Bean
    public JwtAuthenticationFilter jwtAuthenticationFilter() {
        // @bean的方法互相调用是 还是单例的 不会多创建
        return new JwtAuthenticationFilter();
    }


}
